package com.tianqu.tms.www.settle.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.settle.viewhelper.TransferSettleViewHelper;

public class TransferSettleDispatchAction extends TmsAction {

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		TransferSettleViewHelper view = (TransferSettleViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar c1=Calendar.getInstance();
		Calendar c2=Calendar.getInstance();
		Calendar c3=Calendar.getInstance();
		try {
			c1.setTime(df.parse(view.getStartDate()));
			c2.setTime(df.parse(view.getEndDate()));
			c3.setTime(df.parse(DateUtil.getCurrentTime1()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int result=c1.compareTo(c2);
		int result1=c2.compareTo(c3);
		if(result1>0 || result>=0)
		{
			setErrorsToRequest(request, TmsConstants.IM028, null);
			return mapping.getInputForward();
		}
		//不能是同一天 一天只结算一次
		DateFormat df1=new SimpleDateFormat("yyyy-MM-dd");
		Calendar c4=Calendar.getInstance();
		Calendar c5=Calendar.getInstance();
		c4.setTime(df1.parse(view.getStartDate()));
		c5.setTime(df1.parse(view.getEndDate()));
		int result2 =c4.compareTo(c5);
		if(result2==0)
		{
			setErrorsToRequest(request, TmsConstants.IM029, null);
			return mapping.getInputForward();
		}
		request.setAttribute(TmsConstants.VIEWHELP, view);
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
